#include <stdio.h>
#include <string>

using namespace std;

struct GPT_RECORD
{
	char status;
	char fhead;
	char fsector;
	char fcylinder;
	char type;
	char lhead;
	char lsector;
	char lcylinder;
	unsigned int flba;
	unsigned int nblocks;
};

struct CHS
{
	int cylinders;
	int heads;
	int sectors;
};

int main(int argc, char *argv[])
{
	if (argc != 7)
	{
		printf("Usage: makembr sourcembr.bin destmbr.bin status type flba nblocks\n");

		return 0;
	}

	string inputFileName  = argv[1];
	string outputFileName = argv[2];

	FILE *input = fopen(inputFileName.c_str(), "rb");

	int   inputSize = 512;
	char *inputData = new char[inputSize];

	fread(inputData, 1, inputSize, input);
	fclose(input);

	GPT_RECORD *gpt = (GPT_RECORD*)&inputData[446];

	gpt->status    = atoi(argv[3]);
	gpt->type      = atoi(argv[4]);
	gpt->flba	   = atoi(argv[5]); // may need to be adjusted as apparently blocks start at 1?
	gpt->nblocks   = atoi(argv[6]);

	// needs fixing, quick hack for now

	gpt->fhead     = 0;
	gpt->fsector   = gpt->flba + 1;
	gpt->fcylinder = 0;

	gpt->lhead     = 0;
	gpt->lsector   = gpt->flba + gpt->nblocks;
	gpt->lcylinder = 0;


	FILE *output = fopen(outputFileName.c_str(), "wb");
	fwrite(inputData, 1, inputSize, output);
	fclose(output);

	return 0;
}